package com.xqf.text.wxpay.service.impl;


import com.github.wxpay.sdk.WXPayUtil;
import com.xqf.text.wxpay.service.wxPayService;
import com.xqf.text.wxpay.utils.HttpClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author cxk
 * @date 2020/11/4 17:05
 */
@Service
public class wxPayServiceImpl implements wxPayService {

    @Value("${weixin.appid}")
    private String appid;

    @Value("${weixin.partner}")
    private String partner;

    @Value("${weixin.partnerkey}")
    private String partnerkey;

    @Value("${weixin.notifyurl}")
    private String notifyurl;

    /**
     * 通过订单号创建微信二维码
     *
     * @param mapParam
     * @return
     */
    @Override
    public Map nativeCreateCode(Map<String, String> mapParam) {
        Map<String, String> map = new HashMap<>();
        try {
            //应用ID
            map.put("appid", appid);
            //商户ID
            map.put("mch_id", partner);
            //随机字符串
            map.put("nonce_str", WXPayUtil.generateNonceStr());
            //商品描述
            map.put("body", "你最帅");
            //订单号
            map.put("out_trade_no", mapParam.get("orderid"));
            //交易金额  单位为分 这里默认1分钱
            map.put("total_fee", "1");
            //终端ID
            map.put("spbill_create_ip", "127.0.0.1");
            //回调地址
            map.put("notify_url", notifyurl);
            //交易类型  扫码支付
            map.put("trade_type", "NATIVE");
            //将map转成xml会自带签名
            String xml = WXPayUtil.generateSignedXml(map, partnerkey);

            //生成二维码url地址
            String url = "https://api.mch.weixin.qq.com/pay/unifiedorder";

            //提交方式https
            HttpClient httpClient = new HttpClient(url);
            httpClient.setHttps(true);
            //提交参数 xml
            httpClient.setXmlParam(xml);
            //执行请求
            httpClient.post();
            //获取返回数据并返回成map
            String content = httpClient.getContent();
            //转map
            Map<String, String> resultMap = WXPayUtil.xmlToMap(content);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查询支付的订单状态
     *
     * @param orderid
     * @return
     */
    @Override
    public Map queryOrder(String orderid) {
        Map<String, String> map = new HashMap<>();
        try {
            //应用ID
            map.put("appid", appid);
            //商户ID
            map.put("mch_id", partner);
            //随机字符串
            map.put("nonce_str", WXPayUtil.generateNonceStr());
            //订单号
            map.put("out_trade_no", orderid);
            //将map转成xml会自带签名
            String xml = WXPayUtil.generateSignedXml(map, partnerkey);

            //请求地址
            String url = "https://api.mch.weixin.qq.com/pay/orderquery";

            //提交方式https
            HttpClient httpClient = new HttpClient(url);
            httpClient.setHttps(true);
            //提交参数 xml
            httpClient.setXmlParam(xml);
            //执行请求
            httpClient.post();
            //获取返回数据并返回成map
            String content = httpClient.getContent();
            //转map
            Map<String, String> resultMap = WXPayUtil.xmlToMap(content);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
